package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddArticleDto;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddCateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_article")
public class YddArticleController {
    @Autowired
    private IYddArticleService iYddArticleService;
    @Autowired
    private IYddCateService iYddCateService;

    //文章分页展示
    @GetMapping("list")
    public ResultData list(Long start_time, Long end_time, String title, String cateid, int page, int limit){
        //分页
        Page<YddArticle> yddArticlePage=new Page<>(page,limit);
        //条件构造器
        LambdaQueryWrapper<YddArticle> lqw=new LambdaQueryWrapper<>();
        if (start_time!=null && end_time!=null){
            lqw.between(YddArticle::getCreateDate,start_time,end_time);
        }
        lqw.like(StringUtils.isNotEmpty(title),YddArticle::getTitle,title);
        lqw.eq(StringUtils.isNotEmpty(cateid),YddArticle::getCateid,cateid);
        iYddArticleService.page(yddArticlePage,lqw);

        //创建dto分页构建器
        Page<YddArticleDto> yddArticleDtoPage=new Page<>();
        List<YddArticleDto> list=new ArrayList<>();
        //将分页信息复制到dto
        BeanUtils.copyProperties(yddArticlePage,yddArticleDtoPage,"Records");
        List<YddArticle> records = yddArticlePage.getRecords();
        //遍历集合
        records.forEach(yddArticle -> {
            //创建dto对象
            YddArticleDto yddArticleDto=new YddArticleDto();
            //将信息复制到dto
            BeanUtils.copyProperties(yddArticle,yddArticleDto);
            //根据文章的分类id获得分类
            YddCate yddCate=iYddCateService.getById(yddArticle.getCateid());
            //将分类的名称放入dto中
            yddArticleDto.setCatename(yddCate.getCatename());
            list.add(yddArticleDto);
        });
        //创建map集合
        Map<Object,Object> map=new HashMap<>();
        map.put("count",yddArticlePage.getTotal());
        map.put("totalPages",yddArticlePage.getPages());
        map.put("pageSize",yddArticlePage.getSize());
        map.put("currentPage",yddArticlePage.getCurrent());
        map.put("data",list);

        return ResultData.ok("查询成功!",map);
    }

    //根据id查看文章 数据回显
    @GetMapping("rowInfo")
    public ResultData rowInfo(int id){
        YddArticle yddArticle = iYddArticleService.getById(id);
        return ResultData.ok("查询成功",yddArticle);
    }

    //添加文章
    @PostMapping("add")
    public ResultData add(@RequestBody YddArticle yddArticle){
        iYddArticleService.save(yddArticle);
        return ResultData.ok("文章添加成功!","");
    }

    //文章删除
    @GetMapping("del")
    public ResultData del(int id){
        iYddArticleService.removeById(id);
        return ResultData.ok("删除成功!","");
    }

    //修改文章
    @PostMapping("update")
    public ResultData update(@RequestBody YddArticle yddArticle){
        iYddArticleService.updateById(yddArticle);
        return ResultData.ok("修改成功!","");
    }


}
